int main()
{
  int n, fo, co, fd, cd, g;
  cin >> n;   
  cin >> fo; 
  cin >> co; 
  cin >> fd; 
  cin >> cd; 
  cin >> g;   
  
  vector<Nodo> alfa;
  vector<vector<Nodo*> > matrix = crearGrafo(alfa, n, g);

  Nodo* destino = matrix[fd][cd];
  Nodo* origen  = matrix[fo][co];

  bfs(matrix, origen, g);
  imprimirSolucion(matrix, destino);
  
  return 0;
}

vector<vector<Nodo*> > crearGrafo(vector<Nodo> &alfa, int n, int g)
{
  vector<vector<Nodo*> > matrix;
  
  matrix.resize(n);
  for (int i = 0; i < n; i++)
  {
    matrix[i].resize(n);
  }
  
  int k; 
  int l = 1;
  
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)  
    {
      cin >> k;
      Nodo x = Nodo(k, i, j, l);
      alfa.push_back(x);
      l++;      
    }
  }
  k = 0;
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)  
    {
    
      matrix[i][j] = &alfa[k];
      k++;  
    }
  }
  
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)  
    {
      matrix[i][j]->agregarAdyacente(matrix, i, j);
      matrix[i][j]->agregarCheat(matrix, i, j, g);
    }
  }
  
  return matrix;
}

void Nodo::agregarAdyacente(vector<vector<Nodo*> > &matrix, int i, int j)
{       
  int n = matrix.size();
        
        
        for (int k = 1; k <= this->p; k++)  
  { 
    if (tablero(i+k, j, n)){
      this->adyacentes.push_back(make_pair(matrix[i+k][j],0));
    }
    if (tablero(i-k, j, n)){
      this->adyacentes.push_back(make_pair(matrix[i-k][j],0));
    }
    if (tablero(i, j+k, n)){
      this->adyacentes.push_back(make_pair(matrix[i][j+k],0));
    }
    if (tablero(i, j-k, n)){
      this->adyacentes.push_back(make_pair(matrix[i][j-k],0));
    }   
  }
  return;
}


void Nodo::agregarCheat(vector<vector<Nodo*> > &matrix, int i, int j, int g)
{       
  int n = matrix.size();
        
        int x = 1;
        for (int k = this->p+1; k <= this->p+g; k++)  
  { 
    if (tablero(i+k, j, n)){
      this->adyacentes.push_back(make_pair(matrix[i+k][j],x));
    }
    if (tablero(i-k, j, n)){
      this->adyacentes.push_back(make_pair(matrix[i-k][j],x));
    }
    if (tablero(i, j+k, n)){
      this->adyacentes.push_back(make_pair(matrix[i][j+k],x));
    }
    if (tablero(i, j-k, n)){
      this->adyacentes.push_back(make_pair(matrix[i][j-k],x));
    }   
    x++;
  }
  return;
}


void bfs(vector<vector<Nodo*> > matrix, Nodo* origen, int g)
{
  origen->noVisitado = false;
  origen->distancia = 0;
  
  vector<pair<Nodo*, int> > cola;
  cola.push_back(make_pair(origen, g));
  
  for (int k = 0; k < cola.size(); k++)
  {
    Nodo* u = cola[k].first;
    for (int i = 0; i < u->adyacentes.size(); i++)
    {
      if (cola[k].second >= u->adyacentes[i].second)
      {
        Nodo* v = u->adyacentes[i].first;
        if (v->noVisitado)
        {
          v->noVisitado = false;
          v->distancia = u->distancia + 1;
          v->padre = u;
          cola.push_back(make_pair(v, (cola[k].second - u->adyacentes[i].second)));
        }
      }
    }  
  } 
  return;
}


void imprimirSolucion(vector<vector<Nodo*> > &matrix, Nodo* destino)
{
  vector<Nodo*> arreglo;
  arreglo.push_back(destino);
  
  cout << destino->distancia << endl;
  
  while (destino->padre!=NULL)
  {
    arreglo.push_back(destino->padre);
    destino = destino->padre;
  }
  
  for (int i = arreglo.size()-2; i >= 0; i--)
  {
    Nodo* x = arreglo[i];
    int extra;
    for (int j = 0; j < arreglo[i+1]->adyacentes.size(); j++)
    {
      Nodo* y = arreglo[i+1]->adyacentes[j].first;
      if (y->numero == x->numero)
      {
        extra = arreglo[i+1]->adyacentes[j].second;
      }
    }
    cout << arreglo[i]->i << " " << arreglo[i]->j << " " << extra << endl;
  } 
  return;
}


bool tablero(int i, int j, int n)
{
  if (i >= 0 && i <n && j >= 0 && j < n)
  {
    return true;
  }
  return false;
}

